﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ShopBLL;
using ShopModel;

public partial class ClientInfo : BasePage
{
    #region CONSTANTS

    private const string CUSTOMER_ID = "ClientInfo_UserId";

    #endregion CONSTANTS

    #region VARIABLES

    private MembershipUser user;
    private Customer client;

    #endregion VARIABLES

    #region METHODS

    private void LoadCustomerData(Customer client)
    {
        ViewState[CUSTOMER_ID] = client.IDCustomer;

        ctrlCustomerData.City = client.City;
        ctrlCustomerData.CountryID = client.CountryID;
        ctrlCustomerData.Email = client.Email;
        ctrlCustomerData.IsInvoice = client.IsCompany;
        ctrlCustomerData.Name = client.FirstName;
        ctrlCustomerData.Phone = client.Phone;
        ctrlCustomerData.Surname = client.Surname;
        ctrlCustomerData.Newsletter = client.NewsSubscription;

        string[] addressParts = client.Street.Split(new char[] { ' ', '/' }, StringSplitOptions.RemoveEmptyEntries);
        ctrlCustomerData.Building = addressParts[1];
        ctrlCustomerData.Flat = addressParts[2];
        ctrlCustomerData.Street = addressParts[0];

        //clear company section
        ctrlCustomerData.CompanyName = string.Empty;
        ctrlCustomerData.NIP = string.Empty;
        ctrlCustomerData.CompanyPostCode1 = string.Empty;
        //ctrlCustomerData.CompanyPostCode2 = string.Empty;
        ctrlCustomerData.CompanyStreet = string.Empty;
        ctrlCustomerData.CompanyFlat = string.Empty;
        ctrlCustomerData.CompanyBuilding = string.Empty;

        if (client.IsCompany)
        {
            ctrlCustomerData.CompanyName = client.CompanyName;
            ctrlCustomerData.NIP = client.CompanyNIP;
            if (!client.SameAddress)
            {
                ctrlCustomerData.CompanyCity = client.CompanyCity;
                addressParts = client.CompanyStreet.Split(new char[] { ' ', '/' }, StringSplitOptions.RemoveEmptyEntries);
                if (addressParts.Length == 3)
                {
                    ctrlCustomerData.CompanyStreet = addressParts[0];
                    ctrlCustomerData.CompanyBuilding = addressParts[1];
                    ctrlCustomerData.CompanyFlat = addressParts[2];
                }
                else
                {
                    ctrlCustomerData.CompanyStreet = string.Empty;
                    ctrlCustomerData.CompanyBuilding = string.Empty;
                    ctrlCustomerData.CompanyFlat = string.Empty;
                }

                //addressParts = client.CompanyZipCode.Split('-');
                //if (addressParts.Length == 2)
                {
                    ctrlCustomerData.CompanyPostCode1 = client.CompanyZipCode;//[0];
                    //   ctrlCustomerData.CompanyPostCode2 = addressParts[1];
                }
                //else
                {
                    ctrlCustomerData.CompanyPostCode1 = string.Empty;
                    // ctrlCustomerData.CompanyPostCode2 = string.Empty;
                }
            }
        }

        addressParts = client.ZipCode.Split('-');
        ctrlCustomerData.PostCode1 = addressParts[0];
        //ctrlCustomerData.PostCode2 = addressParts[1];

        ctrlCustomerData.SameAddress = client.SameAddress;
        ctrlCustomerData.IsInvoice = client.IsCompany;
    }

    private void ShowInfo(string message, bool isError)
    {
        lblError.Text = message;
        lblError2.Text = message + "<BR />";

        lblError.Visible = true;
        lblError2.Visible = true;

        if (!isError)
        {
            lblError.ForeColor = lblError2.ForeColor = System.Drawing.Color.Green;
        }
    }

    #endregion METHODS

    #region EVENTS

    protected void btnChange_Click(object sender, EventArgs e)
    {
        Customer customer = ctrlCustomerData.Customer;
        customer.IDCustomer = (int)ViewState[CUSTOMER_ID];

        if (OrderManager.Instance.UpdateCustomer(customer))
        {

            ViewState[CUSTOMER_ID] = customer.IDCustomer;
            ctrlCustomerData.Customer = customer;
            //LoadCustomerData(customer);
            ShowInfo(Resources.Language.ChangesSaved, false);
        }
        else
        {
            ShowInfo(Resources.Language.Error_PleaseTryLater, true);
        }

    }

    #endregion EVENTS

    #region PAGE EVENTS

    protected void Page_Load(object sender, EventArgs e)
    {
        user = Membership.GetUser(true);
        if (!IsPostBack)
        {
            client = OrderManager.Instance.GetCustomerByUserId((Guid)user.ProviderUserKey, CommonTools.CurrentLanguageID);
            //LoadCustomerData(client);
            ViewState[CUSTOMER_ID] = client.IDCustomer;
            ctrlCustomerData.Customer = client;

            ctrlCustomerData.Newsletter = client.NewsSubscription;
        }

    }

    #endregion PAGE EVENTS
}
